package br.com.inventario.controller;


import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import javax.servlet.http.HttpServlet;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.data.JRXmlDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sun.istack.internal.logging.Logger;

import br.com.inventario.jdbc.ConnectionFactory;
import br.com.inventario.jdbc.dao.InventarioDAO;
import br.com.inventario.jdbc.dao.ParametroDAO;
import br.com.inventario.jdbc.modelos.Inventario;
import br.com.inventario.relatorios.BoletoServlet;

@Controller
public class InventarioController {
	
	//private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	 
	
	@RequestMapping(method = RequestMethod.GET , value = "pdf")
    public ModelAndView generatePdfReport(ModelAndView modelAndView){
 
        //logger.debug("--------------generate PDF report----------");
		System.out.println("Chegoiu no PDF");
 
        Map<String,Object> parameterMap = new HashMap<String,Object>();
     
        InventarioDAO inventario = new InventarioDAO();       
        
        List<Inventario> InventarioList = inventario.lista();
               
        JRDataSource JRdataSource = new JRBeanCollectionDataSource(InventarioList);
 
        parameterMap.put("datasource", JRdataSource);
 
        //pdfReport bean has ben declared in the jasper-views.xml file
        modelAndView = new ModelAndView("pdfReport", parameterMap);
 
        return modelAndView;
 
    }//generatePdfReport
			
	@RequestMapping ("MostraAdicionaInventario")
	public String mostraadicionainventario (Model model){
		ParametroDAO oparametrodao = new ParametroDAO();
		model.addAttribute("oparametros", oparametrodao.lista());
		return "Inventario/adicionaInventario";
	}

	@RequestMapping ("AdicionaInventario")
	public String adicionaInventario(Inventario oinventario){
		InventarioDAO dao = new InventarioDAO();
		dao.adiciona(oinventario);
		return "redirect:MostraListaInventario";
	}
	
	@RequestMapping("MostraListaInventario")
	public String ListaInventario(Model model){
		InventarioDAO dao = new InventarioDAO();
		List<Inventario> inventarios = dao.lista();
		model.addAttribute("oinventarios", inventarios);
		return "Inventario/listaInventario";
		}
	
	@RequestMapping ("MostraAlteraInventario")
	public String mostraalterainventario (int id, Model model, Model model2){
		InventarioDAO dao = new InventarioDAO();
		ParametroDAO oparametrodao = new ParametroDAO();
		model.addAttribute("oinventario",dao.buscaInventario(id));
		model2.addAttribute("oparametros", oparametrodao.lista());
		return "Inventario/alteraInventario";
	}
	
	@RequestMapping ("AlteraInventario")
	public String alteraInventario(Inventario oinventario){
		InventarioDAO dao = new InventarioDAO();
		dao.altera(oinventario);
		return "redirect:MostraListaInventario";
	}
	
	@RequestMapping ("RemoveInventario")
	public String remove(int id) {
		InventarioDAO dao = new InventarioDAO();
		Inventario oinventario = dao.buscaInventario(id);
		dao.remove(oinventario);
		return "redirect:MostraListaInventario";
	}

}
